package ter.find.pattern;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class MotifTableModel extends AbstractTableModel {
    /**
	 * zo Rabarijaona
	 */
	private boolean DEBUG = false;
	private List<Motif> motifString=new ArrayList<Motif>();
	private int attribMotif=5;
	String Sequences;
	int minSup;
	int idSequences;
	String motifs;
	String itemString = null;
	String idString=null;
	
	private static final long serialVersionUID = 1L;
	private final String[] columnNames = {"Select","Motifs","Id séquence","Support","Séquence/Total"};
    private Object[][] data;
    StringBuffer r = new StringBuffer(200);
    
    public MotifTableModel(List<Motif> motif1){
    	
    	new ArrayList<String>();
		System.out.println("Get motiff");
		data=new Object[motif1.size()][attribMotif];
		for(int i=0;i<motif1.size();i++){
			Motif m=motif1.get(i);
			List<String> listItem=new ArrayList<String>();listItem=m.getItem();
			List<Integer> listId=new ArrayList<Integer>();listId= m.getIdseq();
			String sup=m.getSupport();
			String count=m.getCount();
			//Value to String
			
			
			for(int j=0;j<attribMotif;j++){
				if(j==0){
					System.out.println(listItem.toString());
					itemString=listItem.toString();
					data[i][j]=new Boolean(false);
			
				}
				if(j==1){
					System.out.println(listItem.toString());
					itemString=listItem.toString();
					data[i][j]=listItem.toString();
					r.append("Motifs :");
					r.append(data[i][j].toString());
					r.append("  ");
				}
				if(j==2){
					System.out.println(listId.toString());
					idString=listId.toString();
					data[i][j]=listId.toString();
					r.append("Id Séquence :");
					r.append(data[i][j].toString());
					r.append("    ");
				}
				if(j==3){
					System.out.println(sup);
					data[i][j]=sup;
					r.append("Support :");
					r.append(data[i][j].toString());
					r.append("    ");
				}
				if(j==4){
					System.out.println(count);
					data[i][j]=count;
					r.append("Séquence/Total :");
					r.append(data[i][j].toString());
					r.append("    ");
					r.append('\n');
					r.append('\n');
				}
			}
			//Create a new motif à chaque boucle 
			motifString.add(new Motif(new Boolean(false),itemString, idString, sup,count));
		}
		
    	
    }
    
    public String saveData(){
    	
    	return r.toString();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }


    public Class<? extends Object> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    List<Color> rowColours = Arrays.asList(
            Color.RED,
            Color.GREEN,
            Color.CYAN
        );

        public void setRowColour(int row, Color c) {
            rowColours.set(row, c);
            fireTableRowsUpdated(row, row);
        }

        public Color getRowColour(int row) {
            return rowColours.get(row);
        }
    
    static class MyTableCellRenderer extends DefaultTableCellRenderer {

        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            MotifTableModel model = (MotifTableModel) table.getModel();
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            c.setBackground(model.getRowColour(row));
            return c;
        }
    }


    public boolean isCellEditable(int row, int col) {

        if (col < 2) {
            return true;
        } else {
            return false;
        }
    }


    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

        data[row][col] = value;
        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}